package com.itheima.controller;


import com.itheima.domain.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RequestMapping("student")
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    //查询所有
    @RequestMapping("findAll")
    public ModelAndView findAll(){
        ModelAndView modelAndView=new ModelAndView();
        List<Student> studentList = studentService.findAll();
        modelAndView.addObject("studentList",studentList);
        modelAndView.setViewName("list");
        return modelAndView;
    }
    //查询一个
    @RequestMapping("findById")
    public   ModelAndView findById(Integer id){
        Student student = studentService.findById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("student",student);
        modelAndView.setViewName("update");
        return modelAndView;
    }

    //添加
    @RequestMapping("save")
    public  String save(Student student){
        studentService.save(student);
        return "redirect:findAll";
    }

    //修改
    @RequestMapping("update")
    public  String update(Student student){
        studentService.update(student);
        return "redirect:findAll";
    }
    //删除
    @RequestMapping("delete")
    public  String delete(Integer[] ids){
        studentService.delete(ids);
        return "redirect:findAll";
    }
}
